import {
	mapActions,
	mapMutations
} from 'vuex';
export default {
	computed: {
		appReady() {
			return this.$store.state.appReady;
		}
	},
	methods: {
		...mapMutations({
			setUserState: 'user/setUserState'
		}),
		...mapActions({
			webUserInit: 'user/webUserInit',
			loginByOpenId: 'user/loginByOpenId',
			getServerUserInfo: 'user/getServerUserInfo'
		}),
		handleLogin() {
			return new Promise((resolve, reject) => {
				let self = this;
				this.webUserInit()
					.then(res => {
						console.log(res);
						if (res && res.No) {
							resolve();
						} else {
							return this.loginByOpenId({
								wxAppOpenId: this.$store.state.user.userInfo.openId
							});
						}
					})
					.then(res => {
						console.log(res);
						if (res && res.code == '000000') {
							return this.getServerUserInfo({
								wxAppOpenId: this.$store.state.user.userInfo.openId
							});
						} else {
							reject();
						}
					})
					.then(res => {
						if (res && res.code == '000000') {
							this.setUserState({
								...res.data,
								openId: this.$store.state.user.userInfo.openId
							});
							resolve();
						} else {
							reject();
						}
					})
					.catch(err => {
						console.log(err);
						reject();
					});
			});
		}
	}
}
